﻿using System.Windows.Input;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using HTR.Model;
using MvvmFoundation.Wpf;

namespace HTR.ViewModels
{
    public class CamaraInspeccionViewModel : ObservableObject
    {

        #region Propiedades

        private string _cotaTerrenoAnterior;
        private string _selectedHandle;

        private string _cotaTerrenoText;
        public string CotaTerrenoText
        {
            get { return this._cotaTerrenoText; }
            set { this._cotaTerrenoText = value; RaisePropertyChanged("CotaTerrenoText"); }
        }

        private bool _cotaTerrenoIsEnabled;
        public bool CotaTerrenoIsEnabled
        {
            get { return this._cotaTerrenoIsEnabled; }
            private set { this._cotaTerrenoIsEnabled = value; RaisePropertyChanged("CotaTerrenoIsEnabled"); }
        }

        private bool _insertarIsEnabled;
        public bool InsertarIsEnabled
        {
            get { return this._insertarIsEnabled; }
            private set { this._insertarIsEnabled = value; RaisePropertyChanged("InsertarIsEnabled"); }
        }

        private bool _modificarIsEnabled;
        public bool ModificarIsEnabled
        {
            get { return this._modificarIsEnabled; }
            private set { this._modificarIsEnabled = value; RaisePropertyChanged("ModificarIsEnabled"); }
        }

        private System.Windows.Visibility _editVisibility;
        public System.Windows.Visibility EditVisibility
        {
            get { return _editVisibility; }
            private set { _editVisibility = value; RaisePropertyChanged("EditVisibility"); }
        }

        private System.Windows.Visibility _nonEditVisibility;
        public System.Windows.Visibility NonEditVisibility
        {
            get { return _nonEditVisibility; }
            private set { _nonEditVisibility = value; RaisePropertyChanged("NonEditVisibility"); }
        }

        private CamaraInspeccionCollection _camarasInspeccion;
        public CamaraInspeccionCollection CamarasInspeccion
        {
            get { return this._camarasInspeccion; }
        }

        public ICommand InsertarCamaraInspeccionCmd
        {
            get { return new RelayCommand(this.InsertarCamaraInspeccion); }
        }

        public ICommand ModificarCamaraInspeccionCmd
        {
            get { return new RelayCommand(this.ModificarCamaraInspeccion); }
        }

        public ICommand AceptarModifCamaraInspCmd
        {
            get { return new RelayCommand(this.Aceptar); }
        }

        public ICommand CancelarModifCamaraInspCmd
        {
            get { return new RelayCommand(this.Cancelar); }
        }

        #endregion // Propiedades

        
        #region Constructores

        public CamaraInspeccionViewModel()
        {
            this._camarasInspeccion = new CamaraInspeccionCollection();
            this._cotaTerrenoText = string.Empty;
            this._cotaTerrenoIsEnabled = true;
            this._insertarIsEnabled = true;
            this._modificarIsEnabled = true;
            EditVisibility = System.Windows.Visibility.Collapsed;
            NonEditVisibility = System.Windows.Visibility.Visible;
        }

        #endregion // Constructores


        #region Metodos

        public void InsertarCamaraInspeccion()
        {
            double res = 0;
            if (!double.TryParse(CotaTerrenoText, out res) && !(CotaTerrenoText == string.Empty))
            {
                Application.ShowAlertDialog("Por favor ingrese un valor numérico válido como Cota de Terreno.");
                return;
            }
            if (res < 0)
            {
                Application.ShowAlertDialog("El valor de la Cota de Terreno no puede ser negativo.");
                return;
            }

            DisableInput();

            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            PromptPointOptions pPtOpts = new PromptPointOptions("");
            pPtOpts.Message = "\nIngrese el punto de inserción: ";
            Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();
            PromptPointResult pPtRes = acDoc.Editor.GetPoint(pPtOpts);
            if (pPtRes.Status == PromptStatus.Cancel)
            {
                EnableInput();
                return;
            }
            CamaraInspeccion ci = new CamaraInspeccion(pPtRes.Value, CotaTerrenoText);
            ci.Dibujar();
            CamarasInspeccion.Add(ci.Handle, ci);
            CotaTerrenoText = string.Empty;
            EnableInput();
            return;
        }

        public void ModificarCamaraInspeccion()
        {
            DisableInput();
            this._cotaTerrenoAnterior = CotaTerrenoText;

            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            PromptEntityOptions pEnOpts = new PromptEntityOptions("");
            pEnOpts.Message = "\nSeleccione la Cámara de Inspección a modificar: ";
            // Acá van los filtros para que no se pueda seleccionar cualquier cosa
            Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();
            PromptEntityResult pEnRes = acDoc.Editor.GetEntity(pEnOpts);
            if (pEnRes.Status == PromptStatus.Cancel)
            {
                CotaTerrenoText = this._cotaTerrenoAnterior;
                EditVisibility = System.Windows.Visibility.Collapsed;
                NonEditVisibility = System.Windows.Visibility.Visible;
                EnableInput();
                return;
            }
            try
            {
                this._selectedHandle = pEnRes.ObjectId.Handle.ToString();
                CamaraInspeccion ci = CamarasInspeccion[this._selectedHandle];
                CotaTerrenoText = ci.ValorCotaTerreno;
                EditVisibility = System.Windows.Visibility.Visible;
                NonEditVisibility = System.Windows.Visibility.Collapsed;
            }
            catch
            {
                Application.ShowAlertDialog("Por favor seleccione una Cámara de Inspección.");
            }
            finally
            {
                EnableInput();
            }
            return;
        }

        public void Aceptar()
        {
            CamaraInspeccion ci = CamarasInspeccion[this._selectedHandle];

            ci.Modificar(CotaTerrenoText);

            EditVisibility = System.Windows.Visibility.Collapsed;
            NonEditVisibility = System.Windows.Visibility.Visible;
            CotaTerrenoText = string.Empty;
        }

        public void Cancelar()
        {
            CotaTerrenoText = this._cotaTerrenoAnterior;
            EditVisibility = System.Windows.Visibility.Collapsed;
            NonEditVisibility = System.Windows.Visibility.Visible;
        }

        private void EnableInput()
        {
            InsertarIsEnabled = true;
            ModificarIsEnabled = true;
            CotaTerrenoIsEnabled = true;
        }

        private void DisableInput()
        {
            InsertarIsEnabled = false;
            ModificarIsEnabled = false;
            CotaTerrenoIsEnabled = false;
        }

        #endregion // Metodos

    }
}
